 // 定义游戏状态
 let gameState = {
    currentPlayer: "X",
    board: ['', '', '', '', '', '', '', '', ''],
    winner: null,
    gameOver: false
};




// 游戏逻辑
function playMove(position) {
    if (gameState.board[position] || gameState.gameOver) {
        return;
    }

    gameState.board[position] = gameState.currentPlayer;
    checkWin();
    togglePlayer();
}

function togglePlayer() {
    gameState.currentPlayer = (gameState.currentPlayer === "X") ? "O" : "X";
}

function checkWin() {
    const winningCombinations = [
        [0, 1, 2], [3, 4, 5], [6, 7, 8], // 横向
        [0, 3, 6], [1, 4, 7], [2, 5, 8], // 纵向
        [0, 4, 8], [2, 4, 6] // 对角线
    ];

    for (let combination of winningCombinations) {
        const [a, b, c] = combination;
        if (
            gameState.board[a] &&
            gameState.board[a] === gameState.board[b] &&
            gameState.board[a] === gameState.board[c]
        ) {
            gameState.winner = gameState.currentPlayer;
            gameState.gameOver = true;
            return;
        }
    }

    if (gameState.board.every(cell => cell !== '')) {
        gameState.gameOver = true;
    }
}

// 游戏界面交互
const cells = Array.from(document.querySelectorAll('.cell'));
cells.forEach(cell => {
    cell.addEventListener('click', () => {
        const position = cells.indexOf(cell);
        playMove(position);
        drawBoard();
        updateUI();
        if (gameState.gameOver) {
            showGameOverMessage();
        }
    });
});

function drawBoard() {
    cells.forEach((cell, index) => {
        cell.textContent = gameState.board[index];
    });
}

function showGameOverMessage() {
    if (gameState.winner) {
        // alert(`Player ${gameState.winner} wins!`);
        showWinnerMessage()
    } else {
        showOverMessage();
    }
}
function showWinnerMessage() {
    const winnerOverlay = document.querySelector('.winner-overlay');
    const winnerText = document.getElementById('winner-text');
    winnerText.textContent = `玩家 ${gameState.winner} 赢了!`;
    winnerOverlay.classList.add('show');
}
function showOverMessage() {
    const winnerOverlay = document.querySelector('.winner-overlay');
    const winnerText = document.getElementById('winner-text');
    winnerText.textContent = `游戏平局了!`;
    winnerOverlay.classList.add('show');
}
function resetGame() {
    // 重置游戏状态为初始值
    gameState.currentPlayer = "X";
    gameState.board = ['', '', '', '', '', '', '', '', ''];
    gameState.winner = null;
    gameState.gameOver = false;

    // 清空格子的内容
    const cells = document.querySelectorAll('.cell');
    cells.forEach(cell => {
        cell.textContent = '';
        cell.classList.remove('winner');
    });

    // 隐藏胜利弹框
    const winnerOverlay = document.querySelector('.winner-overlay');
    winnerOverlay.classList.remove('show');
}
// 获取界面元素
const currentPlayerElement = document.getElementById('current-player');
const stepCountElement1 = document.getElementById('step-count1');
const stepCountElement2 = document.getElementById('step-count2');
// 更新界面信息
function updateUI() {
    currentPlayerElement.textContent = gameState.currentPlayer;
    stepCountElement1.textContent = gameState.board.filter(Boolean).length;
    stepCountElement2.textContent = gameState.board.filter(Boolean).length;
}

// 初始化游戏界面
function initGame() {
    drawBoard();
    updateUI();
}
// 初始化游戏
initGame();